//
//  WMEAlbumViewController.m
//  everyDay_duiTang
//
//  Created by 恩叔 on 16/2/19.
//  Copyright © 2016年 enshu. All rights reserved.
//

#import "WMEAlbumViewController.h"
#import "WMEAlbumCollectionViewCell.h"
#import "WMEAlbumModel.h"
#import "WMEContentViewController.h"
#import <Masonry.h>
#import <AFNetworking.h>
#import <MJRefresh.h>
#import "PrefixHeader.pch"
#pragma mark - 专辑封面 系统样式的layout
@interface WMEAlbumViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, retain)UICollectionView *collectionView;
@property (nonatomic, retain)NSMutableArray *albumMArr;
@property (nonatomic, assign)NSInteger count;
@property (nonatomic, retain)NSNumber *maxCount;
@end

@implementation WMEAlbumViewController
- (void)dealloc
{
    [_collectionView release];
    [_albumMArr release];
    [_maxCount release];
    [super dealloc];
}

//- (void)viewWillAppear:(BOOL)animated
//{
//    [super viewWillAppear:animated];
//    if (_albumMArr.count == 0) {
//        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
//        label.textAlignment = NSTextAlignmentCenter;
//        label.text = @"没有数据";
//        label.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1];
//        [self.view addSubview:label];
//    }
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"他的专辑";
    [self createView];
    [self date];
    self.count = 0;
//    [self loadingFunction];
}
- (void)createView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.itemSize = CGSizeMake((WIDTH - 30) / 2, ((WIDTH - 30) / 2) * 3 / 2);
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [self.view addSubview:_collectionView];
    __block __typeof(&*self)ws = self;
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ws.view.mas_left).with.offset(0);
        make.right.equalTo(ws.view.mas_right).with.offset(0);
        make.top.equalTo(ws.view.mas_top).with.offset(0);
        make.bottom.equalTo(ws.view.mas_bottom).with.offset(0);
    }];
    [layout release];
    [_collectionView release];
}
//
- (void)date
{
    NSString *str = _interface;
    //AFN网络请求
    AFHTTPSessionManager *data = [AFHTTPSessionManager manager];
    [data GET:str parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        nil;
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //        NSLog(@"%@", responseObject);
        NSArray *arr = responseObject[@"data"][@"object_list"];
        if(arr.count == 0)
        {
            self.albumMArr = [WMEAlbumModel modelHandlerWithArray:arr];
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64)];
            label.textAlignment = NSTextAlignmentCenter;
            label.text = @"没有数据";
            label.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1];
            [self.view addSubview:label];
        }else{
            NSDictionary *tempDic = arr[0];
            self.maxCount = tempDic[@"count"];
            self.albumMArr = [WMEAlbumModel modelHandlerWithArray:arr];
            _count += 24;
            [_collectionView reloadData];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        nil;
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _albumMArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    [_collectionView registerClass:[WMEAlbumCollectionViewCell class] forCellWithReuseIdentifier:@"albumCell"];
    WMEAlbumCollectionViewCell *albumCell = [_collectionView dequeueReusableCellWithReuseIdentifier:@"albumCell" forIndexPath:indexPath];
    if (_albumMArr.count != 0) {        
        WMEAlbumModel *model = _albumMArr[indexPath.item];
        albumCell.model = model;
        albumCell.backgroundColor = [UIColor whiteColor];
    }
    return albumCell;
}
#pragma mark -点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    WMEAlbumModel *model = _albumMArr[indexPath.item];
    //AFN网络请求
    AFHTTPSessionManager *data = [AFHTTPSessionManager manager];
    NSString *temp = @"http://www.duitang.com/napi/album/detail/?__dtac=%257B%2522_r%2522%253A%2520%2522616818%2522%257D&album_id=77153908&app_code=gandalf&app_version=5.9%20rv%3A150681&device_name=Unknown%20iPhone&device_platform=iPhone7%2C2&include_fields=id%2Cname%2Cdesc%2Cuser%2Clike_count%2Clike_id%2Ccount%2Ccovers%2Cmanagers%2Cmembers%2Cmember_count%2Ccategory%2Cshare_links_2&locale=zh_CN&platform_name=iPhone%20OS&platform_version=9.2&screen_height=667&screen_width=375&user_id=0";
    NSString *str = [temp stringByReplacingOccurrencesOfString:@"album_id=77153908" withString:[NSString stringWithFormat:@"album_id=%@",model.selfId]];
    [data GET:str parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        nil;
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //        NSLog(@"%@", responseObject);
        NSMutableDictionary *dic = responseObject[@"data"];
        WMEContentViewController *contentView = [[WMEContentViewController alloc] init];
        //把专辑详情页的头部数据 和 专辑总个数 传递到下一页
        contentView.albumDic = dic;
        [self.navigationController pushViewController:contentView animated:YES];
        [contentView release];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        nil;
    }];
}

#pragma mark - 加载
//- (void)loadingFunction
//{
//    _collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//        NSString *str = [_interface stringByReplacingOccurrencesOfString:@"start=0" withString:[NSString stringWithFormat:@"start=%ld", _count]];
//        AFHTTPSessionManager *data = [AFHTTPSessionManager manager];
//        [data GET:str parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
//            nil;
//        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//            //        NSLog(@"%@", responseObject);
//            NSArray *arr = responseObject[@"data"][@"object_list"];
//            NSArray *tempArr = [WMEAlbumModel modelHandlerWithArray:arr];
//            [_albumMArr addObject:tempArr];
//            _count += 24;
//            [_collectionView reloadData];
//            if (_count >_maxCount.integerValue) {
//                [_collectionView.mj_footer endRefreshingWithNoMoreData];
//            }else{
//                [_collectionView.mj_footer endRefreshing];
//            }
//
//        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//            nil;
//        }];
//    }];
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
